//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CA = ComponentArt.Web.UI;
using ResourceLabel = CommunityServer.ControlPanel.Controls.ResourceLabel;
using ResourceManager = CommunityServer.ControlPanel.Components.ResourceManager;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;

namespace CommunityServer.ControlPanel.Membership
{
	/// <summary>
	/// Summary description for Roles.
	/// </summary>
	public class Roles : BaseMembershipPage
	{

		#region Members

		protected CA.Grid Grid1;
		protected DropDownList PageList;
		protected CommunityServer.ControlPanel.Controls.ModalLink AddRole;

		int recordCount = 0;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			this.PageList.SelectedIndexChanged += new EventHandler(this.PageList_SelectedIndexChanged);
			base.OnInit(e);
		}

		private void Page_Load(object sender, EventArgs e)
		{
            JavaScript.RegisterRefresh(this);
			if (!Page.IsPostBack)
			{
				Configure();
				Bind();
			}

		}

		private void Configure()
		{


		}

		private void Bind()
		{

			CSContext context = CSContext.Current;

			AddRole.Url = SiteUrls.Instance().ControlPanelRoleAdd;

			string roleEditLink = string.Format("javascript:Telligent_Modal.Open(\"{0}\", 500, 400, reloadRoles);", Globals.GetSiteUrls().UrlData.FormatUrl("membership_ControlPanel_RoleEdit", "## DataItem.GetMember('RoleID').Text ##"));
			string roleSearchLink = Globals.GetSiteUrls().UrlData.FormatUrl("membership_ControlPanel_UserSearch_Role", "## DataItem.GetMember('RoleID').Text ##");
			AddGridHrefTemplate(roleEditLink, "## DataItem.GetMember('Name').Text ##", "Name", "");
			AddGridHrefTemplate(roleSearchLink, ResourceManager.GetString("CP_Membership_Roles_GridCol_ViewMembers"), "RoleID", "CommonTextButton");

			int ps = context.GetIntFromQueryString("ps",10);
			ListItem li = PageList.Items.FindByValue(ps.ToString());
			if(li != null)
				li.Selected = true;

			Grid1.PageSize = Int32.Parse(PageList.SelectedValue);
			ArrayList RolesList = CommunityServer.Components.Roles.GetRoles(0, false, false );

			Grid1.PageSize = ps;
			Grid1.DataSource = RolesList;
			this.recordCount = RolesList.Count;
			Grid1.DataBind();
			Grid1.RecordCount = recordCount;

			// Taken from the BaseGridControl Helper function - In the future this page should inherit from that base class
			foreach(CA.GridColumn gc in Grid1.Levels[0].Columns)
			{
				if(gc.Visible)
				{
					gc.SortedDataCellCssClass = "SortedDataCell";
					//Allows for mock resource manager calls in heading text
					//eg HeadingText="ResourceManager.CP_Photos_GridCol_Name"
					if(gc.HeadingText.StartsWith("ResourceManager."))
						gc.HeadingText = ResourceManager.GetString(gc.HeadingText.Replace("ResourceManager.",""));
					
				}
			}
		}


		private void AddGridHrefTemplate(string href, string text, string dataFieldName, string cssClass)
		{
			CA.ClientTemplate template = new CA.ClientTemplate();
			template.ID = string.Concat("HrefTemplate", dataFieldName);
			string style = string.Empty;
			if(!Globals.IsNullorEmpty(cssClass))
				style = string.Concat(" class=", cssClass); 
			template.Text = String.Format(@"<a href='{0}'{2}>{1}</a>", href, text, style);
			Grid1.ClientTemplates.Add(template);

			foreach(CA.GridColumn gc in Grid1.Levels[0].Columns)
			{
				if(gc.DataField == dataFieldName && gc.Visible)
					gc.DataCellClientTemplateId = template.ID;
			}

		}


		private void PageList_SelectedIndexChanged(object sender, EventArgs e)
		{
			string url  = "{0}?tab={1}&ps={2}";
			url = string.Format(url, Request.Path, Request.QueryString["tab"], PageList.SelectedValue );
			Response.Redirect(url);
		}

	}
}
